import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';

import 'utils/overScrollBehavior.dart';
import 'utils/toast.dart';
import 'routes.dart';

class HomePage extends StatefulWidget {
  final String title;

  HomePage({Key key, this.title}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _counter = 0;
  DateTime _lassPressedAt;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (_lassPressedAt == null ||
            DateTime.now().difference(_lassPressedAt) > Duration(seconds: 2)) {
          _lassPressedAt = DateTime.now();
          Toast.toast(context, msg: '再次点击退出');
          return false;
        }
        return true;
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
          centerTitle: true,
        ),
        body: Center(
          child: ScrollConfiguration(
            behavior: OverScrollBehavior(),
            child: ListView(
              children: <Widget>[
                Center(
                    child: Container(
                        margin: const EdgeInsets.only(top: 16.0),
                        child: Text(
                          'You have pressed the button many times:',
                        ))),
                Center(
                  child: Text(
                    '$_counter',
                    style: Theme.of(context).textTheme.headline4,
                  ),
                ),
                FlatButton(
                  child: Text('open text demo'),
                  textColor: Colors.blue,
                  onPressed: () {
                    Navigator.pushNamed(context, 'new_text');
                    // Navigator.push(context,
                    //     MaterialPageRoute(builder: (context) => TextDemo()));
                  },
                ),
                RandomWordsWidget(),
                ItemButton(label: 'open image demo', router: newImage),
                ItemButton(label: 'open list demo', router: newList),
                ItemButton(label: 'open long list demo', router: newLong),
                ItemButton(label: 'open container demo', router: newContainer),
                ItemButton(
                    label: 'open horizontal demo', router: newHorizontal),
                ItemButton(label: 'open row demo', router: newRow),
                ItemButton(label: 'open grid demo', router: newGrid),
                ItemButton(label: 'open grid more demo', router: newGridEx),
                ItemButton(label: 'open appbar demo', router: newAppbar),
                ItemButton(label: 'open appbar tab demo', router: newTab),
                ItemButton(label: 'open layout demo', router: newLayout),
                ItemButton(label: 'open stack demo', router: newStack),
                ItemButton(label: 'open raisedbutton demo', router: newRaised),
                ItemButton(label: 'open flatbutton demo', router: newFlat),
                ItemButton(label: 'open gesture demo', router: newGest),
                ItemButton(label: 'open dismiss demo', router: newDismiss),
                ItemButton(
                    label: 'open switch and checkbox demo', router: newSwBox),
                ItemButton(label: 'open textfield demo', router: newTextfield),
                ItemButton(label: 'open form demo', router: newForm),
                ItemButton(label: 'open theme demo', router: newTheme),
                ItemButton(
                    label: 'open navigation demo', router: newNavigation),
                ItemButton(
                    label: 'open navigation tab demo',
                    router: newNavigationTab),
                ItemButton(label: 'open drawer demo', router: newDrawer),
                ItemButton(label: 'open progress demo', router: newProgress),
                ItemButton(label: 'open wrap demo', router: newWrap),
                ItemButton(label: 'open aglin demo', router: newAglin),
                ItemButton(label: 'open constrain demo', router: newConstrain),
                ItemButton(label: 'open transform demo', router: newTransform),
                ItemButton(label: 'open clip demo', router: newClip),
                ItemButton(
                    label: 'open scrollview demo', router: newScrollview),
                ItemButton(label: 'open dialog demo', router: newDialog),
              ],
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.add),
          // 提示信息
          tooltip: '请点击FloatingActionButton',
          // 前景色为白色
          foregroundColor: Colors.white,
          // 背景色为紫色
          backgroundColor: Colors.purple,
          // 未点击阴影值
          elevation: 7.0,
          // 点击阴影值
          highlightElevation: 14.0,
          // 点击事件
          onPressed: _incrementCounter,
          // 是否迷你尺寸
          mini: false,
          // 圆形边
          shape: CircleBorder(),
          // 是否动画
          isExtended: false,
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      ),
    );
  }
}

class ItemButton extends StatelessWidget {
  final String label;
  final String router;

  ItemButton({Key key, this.label: '', this.router: ''}) : super(key: key);

  @override
  Widget build(BuildContext context) => FlatButton(
        child: Text(label),
        textColor: Colors.black,
        onPressed: () => Navigator.pushNamed(context, router),
      );
}

class RandomWordsWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 生成随机字符串
    final wordpair = WordPair.random();
    return Center(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(wordpair.toString()),
      ),
    );
  }
}
